package cn.gailvlun.gll.presentation.splash;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.umeng.analytics.MobclickAgent;

import java.util.concurrent.TimeUnit;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.mgr.ShortcutMgr;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.base.Profile;
import cn.gailvlun.gll.net.base.RefreshTokenReq;
import cn.gailvlun.gll.net.base.RefreshTokenRes;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.login.CompleteProfileActivity;
import cn.gailvlun.gll.presentation.login.LoginActivity;
import cn.gailvlun.gll.presentation.login.PhoneBindingActivity;
import cn.gailvlun.gll.presentation.login.WelcomeActivity;
import cn.gailvlun.gll.presentation.main.MainActivity;
import cn.gailvlun.gll.util.LoginUtil;
import cn.gailvlun.gll.util.ProfileUtil;
import cn.gailvlun.gll.util.RxUtil;
import io.reactivex.functions.Consumer;



public class SplashActivity extends BaseActivity {

    @Override
    protected void initComponent() {
        setContentView(R.layout.activity_splash);
    }

    @Override
    protected void createHandler() {

    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        final Profile profile = ProfileUtil.getProfile();



        switch (LoginStateEnum.Companion.getState(profile)) {
            case NONE: {
                
                gotoWelcome();
                break;
            }
            case THIRD_PARTY_AUTHED_NO_BOUND: {
                
                gotoPhoneBinding();
                break;
            }
            case MOBILE_BOUND: {
                
                gotoCompleteProfile();

                break;
            }
            case ACTIVITED: {
                
                gotoMainActivity(profile);
                break;
            }
        }

    }

    private void gotoWelcome() {
        RxUtil.timer(3)
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        WelcomeActivity.openActivity(mContext, true);
                        finish();
                    }
                });

    }


    private void gotoPhoneBinding() {
        PhoneBindingActivity.openActivity(mContext);
        finish();
    }

    private void gotoCompleteProfile() {
        CompleteProfileActivity.openActivity(mContext);
        finish();
    }

    private void gotoMainActivity(final Profile profile) {

        String clientId = null;
        if (profile.getUid() != null) {
            clientId = profile.getUid();
        } else {
            clientId = "+" + profile.getCountry_code() + profile.getMobile();
        }
        HttpMethods.getBaseService()
                .refreshToken(new RefreshTokenReq(clientId, profile.getRefresh_token()))
                .delay(2, TimeUnit.SECONDS)
                .compose(RxUtil.<RefreshTokenRes>applyScheduler())
                .subscribe(new HttpSubscriber<RefreshTokenRes>() {
                    @Override
                    protected void onSuccess(RefreshTokenRes response) {
                        
                        ProfileUtil.refresh(response);
                        MobclickAgent.onProfileSignIn(String.valueOf(profile.getId()));
                        LoginUtil.login(mContext, response.isActivated(), false);
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable RefreshTokenRes response, int code) {
                        if (code == 401) {
                            
                            ShortcutMgr.logOut(mContext);
                            LoginActivity.openActivity(mContext, false);
                            finish();
                        } else {
                            MainActivity.openActivity(mContext, false);
                            finish();
                        }
                    }
                });

    }
}
